"""Sets resolution and status columns to not nullable in case and incident data models

Revision ID: 479024506e05
Revises: 86ec96cd14f8
Create Date: 2022-08-29 16:08:16.679095

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from dispatch.messaging.strings import INCIDENT_RESOLUTION_DEFAULT, CASE_RESOLUTION_DEFAULT


Base = declarative_base()


# revision identifiers, used by Alembic.
revision = "479024506e05"
down_revision = "86ec96cd14f8"
branch_labels = None
depends_on = None


class Incident(Base):
    __tablename__ = "incident"
    id = Column(Integer, primary_key=True)
    resolution = Column(String)


class Case(Base):
    __tablename__ = "case"
    id = Column(Integer, primary_key=True)
    resolution = Column(String)


def upgrade():
    bind = op.get_bind()
    db_session = Session(bind=bind)

    for incident in db_session.query(Incident).filter(Incident.resolution == None):  # noqa: E711
        incident.resolution = INCIDENT_RESOLUTION_DEFAULT

    for case in db_session.query(Case).filter(Case.resolution == None):  # noqa: E711
        case.resolution = CASE_RESOLUTION_DEFAULT

    db_session.commit()

    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column("case", "resolution", existing_type=sa.VARCHAR(), nullable=False)
    op.alter_column("case", "status", existing_type=sa.VARCHAR(), nullable=False)
    op.alter_column("incident", "resolution", existing_type=sa.VARCHAR(), nullable=False)
    op.alter_column("incident", "status", existing_type=sa.VARCHAR(), nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column("incident", "status", existing_type=sa.VARCHAR(), nullable=True)
    op.alter_column("incident", "resolution", existing_type=sa.VARCHAR(), nullable=True)
    op.alter_column("case", "status", existing_type=sa.VARCHAR(), nullable=True)
    op.alter_column("case", "resolution", existing_type=sa.VARCHAR(), nullable=True)
    # ### end Alembic commands ###
